// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 7.10.2006 13:33:28
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   Sudoku.java

package sudoku;

import java.awt.Toolkit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import sudoku.controller.Controller;
import sudoku.model.ChoiceModel;
import sudoku.model.SudokuModel;
import sudoku.ui.HelpUI;
import sudoku.ui.NotifyUI;
import sudoku.ui.SudokuUI;
import sudoku.util.Config;
import sudoku.util.Data;
import sudoku.util.Language;
import sudoku.util.game.Updater;

public class Sudoku {

  private static volatile Config config;

  private static volatile Language language;

  private static volatile Data data;

  public static void main(String args[]) {
    config = new Config();
    language = new Language(config);
    data = new Data(config, language);
    if (args.length == 0) {
      if (!config.isC0()) {
        new NotifyUI(language);
      } else {
        JOptionPane.showMessageDialog(null, language.getMain_error(), language.getMessage(), 1);
      }
    } else if (args[0].equals(config.getVersioon())) {
      config.setC0(true);
      start();
    } else if (args[0].equals("update")) {
      update();
    } else {
      try {
        if ((new File("R-Sudoku.jar")).exists()) {
          Runtime.getRuntime().exec("java -jar R-Sudoku.jar update");
        } else {
          Runtime.getRuntime().exec("java -jar Sudoku.jar update");
        }
      } catch (Exception e) {
        Config.log(e.getClass().toString() + ": " + e.getMessage(), e.getStackTrace());
      }
    }
  }

  private static void start() {
    (new File("Sudoku.jar")).delete();
    (new File("Game.updt.jar")).delete();
    try {
      PrintStream wr = new PrintStream(new FileOutputStream(Config.log, true));
      System.setErr(wr);
    } catch (Exception e) {
      Config.log(e.getClass().toString() + ": " + e.getMessage(), e.getStackTrace());
    }
    config.setNimi(language.getConfig_nimi());
    HelpUI helpUI = new HelpUI(language);
    SudokuModel model = new SudokuModel(language, config, data);
    ChoiceModel choiceModel = new ChoiceModel(config, model);
    Controller controller = new Controller(language, config, data, model, choiceModel);
    SudokuUI m2ng = new SudokuUI(language, config, model, controller, choiceModel);
    m2ng.setData(data);
    m2ng.setHelpUI(helpUI);
    controller.setM2ng(m2ng);
    controller.setHelpUI(helpUI);
    model.setM2ng(m2ng);
    model.setChoiceModel(choiceModel);
    model.setController(controller);
    data.setM2ng(m2ng);
    data.setModel(model);
    data.setController(controller);
    m2ng.pack();
    m2ng.resizeM2ng();
    m2ng.setLocation(config.getLocation_x(), config.getLocation_y());
    m2ng.setVisible(true);
    m2ng.requestFocus();
    if (config.getHetke_kasutaja() == -1) {
      controller.handleEvent("muudaKasutaja");
    }
    m2ng.update(false);
  }

  private static void update() {
    config = new Config();
    Language language = new Language(config);
    JDialog d = new JDialog();
    d.add(new JLabel(language.getUpdater_wait()));
    d.setUndecorated(true);
    d.pack();
    d.setLocation(Toolkit.getDefaultToolkit().getScreenSize().width / 2 - d.getWidth(), Toolkit.getDefaultToolkit().getScreenSize().height
        / 2 - d.getHeight());
    d.setVisible(true);
    File orig = new File("R-Sudoku.jar");
    if (config.isDebug()) {
      Config.log("starting downloading");
    }
    new Updater("jar:http://www.hot.ee/rometpiho/sudoku/R-Sudoku.jar!/", "R-Sudoku.uptd.jar", language);
    if (config.isDebug()) {
      Config.log("downloaded");
    }
    File uptd = new File("R-Sudoku.uptd.jar");
    if (config.isDebug()) {
      Config.log("starting deleting");
    }
    boolean bool;
    for (bool = false; orig.exists() && !bool; bool = orig.delete()) {
      ;
    }
    if (bool || !orig.exists()) {
      uptd.renameTo(orig);
    } else {
      uptd.delete();
    }
    d.dispose();
    if (config.isDebug()) {
      Config.log("deleted");
    }
    try {
      Runtime.getRuntime().exec("java -jar R-Sudoku.jar");
    } catch (Exception e) {
      Config.log(e.getClass().toString() + ": " + e.getMessage(), e.getStackTrace());
    }
  }
}